package com.boot.controller;

import com.boot.Mapper.MonthMapper;
import com.boot.Mapper.YearMapper;
import com.boot.pojo.Month;
import com.boot.pojo.Year;
import com.boot.result.Result;
import com.boot.tool.BaziTool;
import com.boot.tool.JieQi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bazi")
@Api(description = "")
public class BaziController {

    @Autowired
    private YearMapper yearMapper;

    @Autowired
    private MonthMapper monthMapper;

    @ApiOperation("获取年柱")
    @GetMapping("/getNianzhu")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", dataType = "Integer", defaultValue = "1900", paramType = "query")
    })
    public Result<Object> getYear(HttpServletRequest req) {
        BaziTool baziTool = new BaziTool(yearMapper);
        int year = Integer.parseInt(req.getParameter("year"));
        String nianzhu = baziTool.getYear(year);
//        --- 返回 ---
        Result<Object> result = new Result<>();
        result.setCode(1);
        result.setData(nianzhu);
        result.setMessage("success");
        return result;
    }

    @ApiOperation("获取月柱")
    @GetMapping("/getYuezhu")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "日期", dataType = "String", defaultValue = "1995-12-04 13:00:00", paramType = "query")
    })
    public Result<Object> getMonth(HttpServletRequest req) throws ParseException {
//        --- 接收 ---
        String date = req.getParameter("date");
//        --- 处理 ---
        int year = Integer.parseInt(date.split("-")[0]);
        int index = (year - 3) % 60;
        List<Year> years = yearMapper.getYear(index);
        Year year1 = years.get(0);
        String yearTiangan = year1.getTiangan();  // 此处获取到年柱
        JieQi jieQi = new JieQi();
        int monthIndex = jieQi.getNontLi(date);
        List<Month> months = monthMapper.selectMonth(yearTiangan, monthIndex);
        Month month = months.get(0);
        String ganzhi = month.getGanzhi(); // 此处获取到月柱
        BaziTool baziTool = new BaziTool();
        int diff = (int) baziTool.getDayDiff(date);
        int dayIndex = diff % 60;
        List<Year> years1 = yearMapper.getYear(dayIndex);
        Year year2 = years1.get(0);
        String dayGanzhi = year2.getGanzhi(); // 此处获取到日柱
        String dayTiangan = year2.getTiangan();
        String shichen = baziTool.timeIndex(date); // 此处获取时辰
        HashMap<String, Object> info = new HashMap<>();
        info.put("年柱", year1.getGanzhi());
        info.put("月柱", ganzhi);
        info.put("日柱", dayGanzhi);
        info.put("时柱", dayTiangan + shichen);
//        --- 返回 ---
        Result<Object> result = new Result<>();
        result.setCode(1);
        result.setData(info);
        result.setMessage("success");
        return result;
    }

    @ApiOperation("获取节气")
    @GetMapping("/getJieqi")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", dataType = "Integer", defaultValue = "1950", paramType = "query")
    })
    public Result<Object> getJieqi(HttpServletRequest req) {
        int year = Integer.parseInt(req.getParameter("year"));
        JieQi jieQi = new JieQi();
        Map<String, String> hashMap = jieQi.getYearTermMap(year);
//        --- 返回 ---
        Result<Object> result = new Result<>();
        result.setCode(1);
        result.setData(hashMap);
        result.setMessage("success");
        return result;
    }
}
